package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.INoteBizService;
import com.kk.xhr.model.entity.Note;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 笔记管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "笔记管理", tags = "笔记管理")
@RequestMapping("/note")
public class NoteController {
    private final Executor adminControllerBizExecutor;
    private final INoteBizService noteBizService;

    @ApiOperation(value = "查询我的笔记")
    @GetMapping("mine")
    public CompletionStage<ApiResult<List<Note>>> queryMine() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> ApiResult.success(noteBizService.query(userId))
            , adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加笔记")
    @PostMapping
    public CompletionStage<ApiResult<Void>> add(@RequestBody Note note) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            noteBizService.add(userId, note.getContent());
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改笔记")
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody Note note) {
        return CompletableFuture.supplyAsync(() -> {
            noteBizService.modify(id, note.getContent());
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "删除笔记")
    @DeleteMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() -> {
            noteBizService.remove(id);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
